package com.ruoyi.service.impl;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.dto.ReportDataDto;
import com.ruoyi.mapper.HhReportMapper;
import com.ruoyi.service.IHhReportService;
import com.ruoyi.utils.ReportUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenjc
 * @since 2021/03/24 17:07
 */
@Service
public class HhReportServiceImpl implements IHhReportService {

    @Autowired
    private HhReportMapper hhReportMapper;

    @Override
    public ReportDataDto getYearData(ReportDataDto reportDataDto) {
        String year = reportDataDto.getYear();
        if (StringUtils.isBlank(year)) {
            year = DateUtils.parseDateToStr(DateUtils.YYYY, new Date());
        }
        reportDataDto.setBeginDate(year + ReportUtil.firstMonth);
        reportDataDto.setEndDate(year + ReportUtil.lastMonth);
        List<ReportDataDto> reportDataDtos = hhReportMapper.selectYearWorkerReport(reportDataDto);
        return ReportUtil.formatterLineData(reportDataDtos);
    }

    @Override
    public Map<String, Object> getTypeMonthReport(ReportDataDto reportDataDto) {
        String year = reportDataDto.getYear();
        if (StringUtils.isBlank(year)) {
            year = DateUtils.parseDateToStr(DateUtils.YYYY, new Date());
        }
        reportDataDto.setBeginDate(ReportUtil.getFirstDateTime(year).toString());
        reportDataDto.setEndDate(ReportUtil.getLastDateTime(year).toString());
        List<ReportDataDto> monthReports = hhReportMapper.selectMonthReport(reportDataDto);
        List<ReportDataDto> typeReports = hhReportMapper.selectTypeReport(reportDataDto);
        Map<String, Object> resultData = new HashMap<>();
        resultData.put("monthData", ReportUtil.formatterMonthData(monthReports));
        resultData.put("typeData", ReportUtil.formatterPieData(typeReports));
        return resultData;
    }

    @Override
    public ReportDataDto getMonthReport(ReportDataDto reportDataDto) {
        return null;
    }

    @Override
    public List<ReportDataDto> getWorkerDetailReport(ReportDataDto reportDataDto) {
        String year = reportDataDto.getYear();
        if (StringUtils.isBlank(year)) {
            year = DateUtils.parseDateToStr(DateUtils.YYYY, new Date());
        }
        reportDataDto.setBeginDate(ReportUtil.getFirstDateTime(year).toString());
        reportDataDto.setEndDate(ReportUtil.getLastDateTime(year).toString());
        List<ReportDataDto> reportDataDtos = hhReportMapper.selectWorkerDetailReport(reportDataDto);
        return ReportUtil.formatterWorkerData(reportDataDtos);
    }
}
